<!--
 * @Author: 谢陈洁
 * @Date: 2025-06-09 15:28:14
 * @LastEditors: 谢陈洁
 * @LastEditTime: 2025-07-19 13:57:48
 * @Description: file content
-->
<template>
  <DynamicForm
    ref="dynamicFormRef"
    :form-config="formItems"
    v-model="formData"
    @submit-success="handleSubmit"
    :showSubmitButton="true"
  >
    <!-- 自定义底部 -->
    <!-- <template #footer="{ form }">
      <div>
        <ResetButton @click="handleReset(form)"></ResetButton>
        <BaseButton type="submit" @click="handleSubmit(form)"></BaseButton>
      </div>
    </template> -->
  </DynamicForm>
</template>
<script setup lang="ts">
  import { fetchArticleDetail, updateArticle } from '@/services/apis/article'
  import { formItems } from './config'
  const router = useRouter()
  const route = useRoute()
  const formData = ref()
  const dynamicFormRef = ref()
  const handleSubmit = async (formValue: any) => {
    console.log('提交信息', formValue)
    const params = { ...formValue }
    const formData = new FormData()
    for (const key in params) {
      if (key !== 'cover') {
        formData.append(key, params[key])
      } else {
        formData.append('cover', params.cover[0].raw || params.cover[0].url)
      }
    }
    const result = await updateArticle(formData)
    if (result) {
      ElMessage.success('保存成功')
      router.push('/content/article')
    } else {
      ElMessage.error('保存失败')
    }
  }
  onMounted(async () => {
    const id = route.query.id as string
    if (id) {
      const res = await fetchArticleDetail(id)
      formData.value = {
        ...res,
        category_id: res.category_id.toString(),
        fileList: [{ url: res.cover }],
        cover: [{ url: res.cover }]
      }
    }
  })
</script>
<style lang="scss" scoped></style>
